package service

import (
	"strconv"
	"strings"
	"time"
	"yak.admin/src/entity"
	"yak.admin/src/util"
)

type MessageService struct {
	UserId uint
}

func (msg *MessageService) SetDrugMessage(strTime string) error {
	db := util.GetMysqlPool().Handler()
	tpl := util.GetSettingValue("drug_message")
	message := entity.Message {
		UserId: msg.UserId,
		Message: strings.Replace(tpl, "{time}", strTime, 1 ),
		CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
	}
	table := "message_" + strconv.Itoa(int(msg.UserId % 10))
	return db.Table(table).Create(&message).Error
}

func (msg *MessageService) SetDeviceMessage(strTime string) error {
	db := util.GetMysqlPool().Handler()
	tpl := util.GetSettingValue("device_message")
	message := entity.Message {
		UserId: msg.UserId,
		Message: strings.Replace(tpl, "{time}", strTime, 1 ),
		CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
	}
	table := "message_" + strconv.Itoa(int(msg.UserId % 10))
	return db.Table(table).Create(&message).Error
}
